package com.rivulet.dawn.service.service.archive.impl;

import com.rivulet.dawn.api.model.archive.EventRecordModel;
import com.rivulet.dawn.api.request.AllDeleteRequest;
import com.rivulet.dawn.api.request.archive.EventRecordQueryRequest;
import com.rivulet.dawn.api.request.archive.EventRecordSaveRequest;
import com.rivulet.dawn.api.request.archive.EventRecordUpdateRequest;
import com.rivulet.dawn.api.response.archive.EventRecordDetailResponse;
import com.rivulet.dawn.api.response.archive.EventRecordSearchResponse;
import com.rivulet.dawn.api.tools.Page;
import com.rivulet.dawn.service.mapper.EventRecordMapper;
import com.rivulet.dawn.service.service.archive.EventRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author: xjc
 * @date: 2022/8/16 16:16
 **/
@Service
public class EventRecordServiceImpl implements EventRecordService {
    @Resource
    private EventRecordMapper eventRecordMapper;
    @Override
    public Integer create(EventRecordSaveRequest request) {
        EventRecordModel model = new EventRecordModel();
        BeanUtils.copyProperties(request,model);
        return eventRecordMapper.insert(model);
    }

    @Override
    public void delete(AllDeleteRequest request) {
        if(!CollectionUtils.isEmpty(request.getIds())){
            eventRecordMapper.delete(request.getIds());
        }
    }

    @Override
    public Integer update(EventRecordUpdateRequest request) {
        EventRecordModel model = new EventRecordModel();
        BeanUtils.copyProperties(request,model);
        return eventRecordMapper.updateByPrimaryKey(model);
    }

    @Override
    public Page<EventRecordSearchResponse> search(EventRecordQueryRequest request) {
        List<EventRecordModel> modelList = eventRecordMapper.search(request);
        List<EventRecordSearchResponse> respList =new ArrayList<>();
        for(EventRecordModel model:modelList){
            EventRecordSearchResponse resp = new EventRecordSearchResponse();
            BeanUtils.copyProperties(model,resp);
            respList.add(resp);
        }
        return new Page<>(Long.valueOf(respList.size()),respList);
    }

    @Override
    public EventRecordDetailResponse detail(Long id) {
        EventRecordModel model = eventRecordMapper.selectByPrimaryKey(id);
        EventRecordDetailResponse response =new EventRecordDetailResponse();
        BeanUtils.copyProperties(model,response);
        return response;
    }
}
